package com.android.locationapi.maps;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.RelativeLayout;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


public class ShowMap extends MapActivity {
	
	private MyLocationOverlay locationOverlay;
	
	 
	 private LocationManager lm;
	 GeoPoint gp;
	
	private MapController mapController;
	private MapView mapView;
	private LocationManager locationManager;
	Location location ;

	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.main); // bind the layout to the activity

		// create a map view
		RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.setStreetView(true);
		mapController = mapView.getController();
		mapController.setZoom(14); // Zoom 1 is world view
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new GeoUpdateHandler());
		
		
		//List<Overlay> mapOverlays = mapView.getOverlays();
		
		 List<Overlay> mapOverlays = mapView.getOverlays();
		    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
		    
		   // locationOverlay.enableMyLocation();

		  lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,120 * 1000, 0, new GeoUpdateHandler() );
		   Location lastKnownLoc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		  
		   Location lastLocation = lm.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
		    if ( lastLocation != null )
		    {
		   
try{
		   
		     int longTemp = (int)(lastKnownLoc.getLongitude()* 1000000);
		     int latTemp = (int)(lastKnownLoc.getLatitude() * 1000000);
		     gp =  new GeoPoint(latTemp, longTemp);
}
catch (NullPointerException e)
{
	
}
		    }
		   //GeoPoint point = locationOverlay.getMyLocation();
		   GeoPoint point2 = new GeoPoint(35410000, -739960000);
		    OverlayItem overlayitem2 = new OverlayItem(gp, "Hello people", "I am Upen!");
		
		    
			itemizedoverlay.addOverlay(overlayitem2);
		    mapOverlays.add(itemizedoverlay);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public class GeoUpdateHandler implements LocationListener {
		
		@Override
		public void onLocationChanged(Location location) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			GeoPoint point = new GeoPoint(lat, lng);
			mapController.animateTo(point); 
			//mapController.setCenter(point);
			
			
			
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}
}